

setClass("ZeroCouponBond",
		representation=list(
			
				Nominal="numeric"
				
				
		),contains=c("SituationDate","EffectiveDate","MaturityDate","BasicInstrument","Exchange","DayCountConvention","Price")
)



ZeroCouponBond <- function(Nominal,Currency,Name,SituationDate,MaturityDate,DayCountConvention,Exchange){
	
	EffectiveDate <- SituationDate
	new("ZeroCouponBond",
			Nominal=Nominal,
			Currency=Currency,
			Name=Name,
			SituationDate=SituationDate,
			EffectiveDate=EffectiveDate,
			MaturityDate=MaturityDate,
			DayCountConvention=DayCountConvention,
			Exchange=Exchange)
}

# B1 <- ZeroCouponBond(500,"CHF","BBB",Sys.Date(),DateYMD(2020,11,30),"30/360","SWISSEXCHANGE")
# object <- ZeroCouponBond(500,"CHF","Swiss Govi Bond",Sys.Date(),DateYMD(2020,11,30),"30/360","SWISSEXCHANGE")

setMethod("show","ZeroCouponBond",function(object){
			cat("Zero Coupon Bond\n")
			cat("Name:         ",name(object),"\n",sep="")
			cat("Currency:     ",currency(object),"\n",sep="")
			cat("MaturityDate: ",as.character(object@MaturityDate),"\n",sep="")
			cat("Nominal:      ",cashNumberToText(object@Nominal),"\n",sep="")
		})

setMethod("generateCashFlows","ZeroCouponBond",function(object){
			MaturityDate <- object@MaturityDate
			interestPaymentDates <- getTradingDay(MaturityDate,Exchange=object@Exchange,Direction=1)
			CashFlow <- object@Nominal
			CashFlow(object@Currency,object@Name,interestPaymentDates,CashFlow)
		})

setMethod("presentValue",signature=c("ZeroCouponBond","DiscountFactor"),function(object,arg1){
			presentValue(generateCashFlows(object),arg1)
		})

setMethod("presentValue",signature=c("ZeroCouponBond","ZeroRateCurve"),function(object,arg1){
			presentValue(generateCashFlows(object),arg1)
		})

setMethod("duration",signature=c("ZeroCouponBond","DiscountFactor"),function(object,DF){
			duration(generateCashFlows(object),DF)
		})

setMethod("duration",signature=c("ZeroCouponBond","ZeroRateCurve"),function(object,DF){
			duration(generateCashFlows(object),DF)
		})

setMethod("convexity",signature=c("ZeroCouponBond","DiscountFactor"),function(object,DF){
			convexity(generateCashFlows(object),DF)
		})

setMethod("convexity",signature=c("ZeroCouponBond","ZeroRateCurve"),function(object,DF){
			convexity(generateCashFlows(object),DF)
		})

setMethod("dv01",signature=c("ZeroCouponBond","DiscountFactor"),function(object,arg1){
			dv01(generateCashFlows(object),arg1)
		})

setMethod("dv01",signature=c("ZeroCouponBond","ZeroRateCurve"),function(object,arg1){
			dv01(generateCashFlows(object),arg1)
		})

setMethod("dv01empirical",signature=c("ZeroCouponBond","DiscountFactor"),function(object,arg1){
			dv01empirical(generateCashFlows(object),arg1)
		})

setMethod("dv01empirical",signature=c("ZeroCouponBond","ZeroRateCurve"),function(object,arg1){
			dv01empirical(generateCashFlows(object),arg1)
		})

setMethod("estimateZeroSpread",signature=c("ZeroCouponBond","ZeroRateCurve"),function(object,DF){
			Price <- price(object)
			MarketValue <- Price * object@Nominal / 100
			f <- function(Spread){
				MarketValue - presentValue(object,DF + Spread)
			}
			Spread <- uniroot(f,c(-1000,1000))$root
			Spread
		})

setMethod("estimateZeroSpread",signature=c("ZeroCouponBond","DiscountFactor"),function(object,DF){
			estimateZeroSpread(object,as(DF,"ZeroRateCurve"))
		})

setMethod("estimateIRR",signature=c("ZeroCouponBond"),function(object){
			Price <- price(object)
			MarketValue <- Price * object@Nominal / 100
			CF <- generateCashFlows(object)
			n <- dim(CF)[1]
			MaturityDate <- getDate(CF)[n]
			Currency <- currency(object)
			SituationDate <- situationDate(object)
			Date <- getDate(CF)
			f <- function(Rate){
				ZR <- ZeroRateCurve(SituationDate,Currency,object@Name,c(SituationDate,Date),rep(Rate,n+1))
				MarketValue - presentValue(object,ZR)
			}
			Rate <- uniroot(f,c(0,1000))$root
			Rate
		})


